/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-25 13:02:04
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.BpJobSearchBean;
import com.joysuch.wwyt.bp.entity.BpJob;
import com.joysuch.wwyt.bp.service.BpJobService;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.repeatsubmit.NoRepeatSubmit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/bp/job")
@Api(tags = "岗位")
public class BpJobController {

    private static final Logger logger = LoggerFactory.getLogger(BpJobController.class);

    @Autowired
    private BpJobService bpJobService;

    @PostMapping("add")
    @RequiresPermissions("bp:job:add")
    @NoRepeatSubmit(paramNames = "name")
    public ResultBean add(@RequestBody @Valid BpJob entity) {
        try {
            bpJobService.add(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, e.getMessage());
        }
    }

    @PostMapping("update")
    @RequiresPermissions("bp:job:update")
    public ResultBean update(@RequestBody @Valid BpJob entity) {
        try {
            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            bpJobService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#" + entity.getId(), e);
            return ResultBean.fail(101, e.getMessage());
        }
    }

    @GetMapping("page")
    public ResultBean page(Pageable page, BpJobSearchBean bpJobSearchBean) {
        try {
            page = PageDataUtils.addOrderByDesc(page, "id");
            Page<BpJob> data = bpJobService.findByPage(page, bpJobSearchBean);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @RequestMapping("simple-list")
    public Object simpleList() {
        try {
            List<SimpleSelectListBean> data = bpJobService.getList();
            return ResultBean.pageData(data, data.size());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @RequestMapping("simple-page")
    public Object simplePage(Pageable list) {
        try {
            List<SimpleSelectListBean> data = bpJobService.getList();
            return ResultBean.pageData(data, data.size());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @PostMapping("delete")
    @ApiOperation("单个删除岗位")
    @RequiresPermissions("bp:job:delete")
    public ResultBean delete(Long id) {
        bpJobService.delete(id);
        return ResultBean.success("");
    }

    @PostMapping("deleteByIds")
    @ApiOperation("批量删除岗位")
    public ResultBean deleteByIds(@RequestBody List<Long> ids) {
        List<String> message = bpJobService.deleteByIds(ids);
        return ResultBean.success(message);
    }


    @GetMapping("editAlarmTemplate")
//    @RequiresPermissions("bp:data-access:editAlarmTemplate")
    @ApiOperation("添加岗位报警模板")
    public ResultBean editAlarmTemplate(Long jobId, Long templateId, String templateName) {
        BpJob job = bpJobService.editAlarmTemplate(jobId, templateId, templateName);
        return ResultBean.success(job);
    }
}
